﻿using CustomPixelShaderEffect.AttributeUsageFloder;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;


namespace CustomPixelShaderEffect.Shaders
{
    public enum ShaderType
    {
        // 无
        None,
        [ShaderInfo("阈值效果", "ms-appx:///Shaders/ThresholdEffect.bin")]
        ThresholdEffect,
        [ShaderInfo("颜色替换效果", "ms-appx:///Shaders/ColorReplacementEffect.bin")]
        ColorReplacementEffect,
        [ShaderInfo("渐变映射效果", "ms-appx:///Shaders/GradientMappingEffect.bin")]
        GradientMappingEffect,
        [ShaderInfo("选区效果", "ms-appx:///Shaders/ConstituencyEffect.bin")]
        ConstituencyEffect,
        [ShaderInfo("碎片效果", "ms-appx:///Shaders/FragmentEffectEffect.bin")]
        FragmentEffect,
        [ShaderInfo("曲线模糊", "ms-appx:///Shaders/CurveBlurEffect.bin")]
        CurveBlurEffect,
        [ShaderInfo("消融效果", "ms-appx:///Shaders/DissolveEffect.bin")]
        DissolveEffect,
    }

    public static class ShaderExtensions
    {
        //@Static
        public static async Task<byte[]> LoadAsync(this ShaderType type)
        {
            string path = type.GetPath();
            if (string.IsNullOrEmpty(path))
                return null;
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path));
            IBuffer buffer = await FileIO.ReadBufferAsync(file);
            return buffer.ToArray();
        }
        private static string GetPath(this ShaderType type)
        {
            var typeInfo = (TypeInfo)type.GetType();
            FieldInfo fi = typeInfo.GetField(type.ToString());

            var info = fi.GetCustomAttribute<ShaderInfoAttribute>();
            if (info != null)
            {
                return info.Path;
            };
            return null;
        }
    }
}